package jjwt.study.Controller;

import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * 描述:
 *
 * @author Ychiha
 * @version 1.0
 * @date 14/1/2022 下午6:49
 */
@RestController
@RequestMapping("/user")    //因为资源配置处配置了antMatchers("/user/**"); 标志该Controller下的资源由资源配置负责放行
public class UserController {

    /**
     * 获取当前用户
     * @param authentication
     * @return
     */
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request){
        String header = request.getHeader("Authorization");
        String token = header.substring(header.indexOf("bearer") + 7);
        return Jwts.parser()
                //密钥
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                //解析
                .parseClaimsJws(token)
                .getBody();
    }
}
